<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Recommend_m extends FOX_Model
{

    function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $id
     * @return mixed
     * 查询单条
     */
    public function get_recommend_by_id($id)
    {

        $this->db->select('*');
        $this->db->from('recommend');
        $this->db->where('id', $id);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function get_recommend_by_mobile($mobile)
    {

        $this->db->select('id');
        $this->db->from('recommend');
        $this->db->where('mobile', $mobile);
        $query = $this->db->get();
        return $query->row_array();
    }

    /**
     * @param $id
     * @param $data
     * @return bool
     * 修改日志信息
     */
    public function sava_recommend($id, $data)
    {

        $this->db->where('id', $id);
        $this->db->update('recommend', $data);
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    /**
     * @param $data
     * @return bool
     * 新增日志
     */
    public function add_recommend($data)
    {
        $this->db->insert('recommend', $data);
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有日志
     */
    public function get_all_recommend($start, $limit, $user_id = "0")
    {
        $this->db->select('*');
        $this->db->from('recommend');
        $this->db->limit($limit, $start);
        if (!empty($user_id)) {
            $this->db->where('uid', $user_id);
        }
        $this->db->order_by('id', 'DESC');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    /**
     * 获取启用的规则
     */
    public function get_recommend_by_uid($uid)
    {
        $sql   = "SELECT * FROM {$this->db->dbprefix}recommend  where uid = $uid ORDER BY o asc";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    /**
     * @param $where
     * @return bool
     * 删除日志
     */
    public function delete_recommend_by($where)
    {
        $this->db->where($where)->delete('recommend');
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    //删除日志
    public function delete_recommend_by_id($id)
    {
        $this->db->where('id', $id)->delete('recommend');
        return ($this->db->affected_rows() > 0) ? true : false;
    }
}
